package com.example.edu.Mapper;

import com.example.edu.Dto.ArticleListReturnDto;
import com.example.edu.Dto.PersonalArticleListReturnDto;
import com.example.edu.Entity.Article;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.List;

@Mapper
public interface ArticleMapper {
    @Select("select a.article_id,a.article_title,a.is_publish from article_picture ap join article a on ap.article_id = a.article_id " +
            "where ap.picture_id = #{picture_id} and ap.telephone = #{telephone} order by a.article_id")
    List<Article> selectRelationArticles(@Param("telephone") String telephone, @Param("picture_id") int picture_id);

    @Insert("insert into article(article_title, create_time, anti_title, article_introduce, author_telephone) " +
            "values (#{article.article_title} , now(), #{article.anti_title}, #{article.article_introduce}, #{article.author_telephone})")
    @Options(useGeneratedKeys = true, keyProperty = "article_id") // 自动获取主键
    void insertDraft(@Param("article") Article article);

    @Insert("insert into article(article_title, create_time, anti_title, article_introduce, author_telephone,is_publish) " +
            "values (#{article.article_title} , now(), #{article.anti_title}, #{article.article_introduce}, #{article.author_telephone},1)")
    @Options(useGeneratedKeys = true, keyProperty = "article_id") // 自动获取主键
    void insertArticle(@Param("article") Article article);

    @Delete("delete from article where article_id = #{article_id}")
    void delete(@Param("article_id") int article_id);

    @Update("update article set article_title = #{article_title} , anti_title = #{anti_title} , article_introduce = #{article_introduce} " +
            "where article_id = #{article_id}")
    void update(@Param("article_title") String title,@Param("anti_title") String anti_title,@Param("article_introduce") String introduce,@Param("article_id") int article_id);

    @Select("select count(*) from article where author_telephone = #{telephone} and is_publish = 0")
    int getDraftNums(@Param("telephone") String telephone);

    @Select("select count(*) from article where author_telephone = #{telephone} and is_publish = 1")
    int getArticleNums(@Param("telephone") String telephone);

    @Select("select count(*) from article where is_publish = 1")
    int getAllArticleNums();

    @Select("select count(*) from article " +
            "where author_telephone = #{telephone} and is_publish = 0 " +
            "and article_title like concat(#{put},'%') or article.anti_title like concat(#{anti_put},'%') " +
            "or article_title like concat('%',#{put},'%') ")
    int getDraftNumsByPut(@Param("telephone") String telephone,@Param("put") String put,@Param("anti_put") String anti_put);

    @Select("select article_id,article_title,view_times,create_time,article_introduce from article " +
            "where author_telephone = #{telephone} and is_publish = 0 " +
            "order by article_id " +
            "limit #{count} " +
            "offset #{start}")
    List<PersonalArticleListReturnDto> getDrafts(@Param("telephone") String telephone, @Param("start") int start, @Param("count") int count);

    @Select("select article_id,article_title,view_times,create_time,article_introduce from article " +
            "where author_telephone = #{telephone} and is_publish = 1 " +
            "order by article_id " +
            "limit #{count} " +
            "offset #{start}")
    List<PersonalArticleListReturnDto> getArticles(@Param("telephone") String telephone, @Param("start") int start, @Param("count") int count);

    @Select("select article_id,article_title,view_times,create_time,article_introduce from article " +
            "where author_telephone = #{telephone} and is_publish = 0 " +
            "and article_title like concat(#{put},'%') or article.anti_title like concat(#{anti_put},'%') " +
            "or article_title like concat('%',#{put},'%') " +
            "order by article_id " +
            "limit #{count} " +
            "offset #{start}")
    List<PersonalArticleListReturnDto> getDraftsByPut(@Param("telephone") String telephone,@Param("put") String put,@Param("anti_put") String anti_put,@Param("start") int start,@Param("count") int count);

    List<PersonalArticleListReturnDto> getArticlesByUnion(@Param("telephone") String telephone, @Param("put") String put, @Param("anti_put") String anti_put, @Param("begin") Timestamp begin,@Param("end") Timestamp end);

    List<ArticleListReturnDto> getArticlesByUnion2(@Param("put") String put, @Param("anti_put") String anti_put, @Param("begin") Timestamp begin, @Param("end") Timestamp end);

    @Update("update article set view_times = view_times + 1 where article_id = #{article_id}")
    void addView(@Param("article_id") int article_id);

    @Update("update article set is_publish = 1 where article_id = #{article_id}")
    void publish(@Param("article_id") int article_id);

    @Update("update article set is_publish = 0 where article_id = #{article_id}")
    void recall(@Param("article_id") int article_id);

    @Select("select article_id,article_title,view_times,create_time,article_introduce,nik_name from article join user on author_telephone = telephone where is_publish = 1 order by view_times limit 5")
    List<ArticleListReturnDto> getHot();

    @Select("select article_id,article_title,view_times,create_time,article_introduce,nik_name from article join user on author_telephone = telephone where is_publish = 1 limit #{count} offset #{start}")
    List<ArticleListReturnDto> getALLArticles(@Param("start") int start, @Param("count") int count);
}
